
import { _decorator, Button, Component, Label, Node, Sprite } from 'cc';
import { UnlockType } from '../fight/gun/SecondaryGun';
import { GameData } from '../system/GameData';
import { AppControll, PopView } from '../manager/AppControll';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = NewComponent
 * DateTime = Wed May 17 2023 17:33:09 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = NewComponent.ts
 * FileBasenameNoExtension = NewComponent
 * URL = db://assets/script/system/NewComponent.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('UnlockSecondGun')
export class UnlockSecondGun extends Component {
    @property(Node)
    mBody: Node = null;
    @property(Label)
    mLbTitle: Label = null;
    @property(Label)
    mLbContent: Label = null;
    @property(Node)
    mNdspend: Node = null;
    @property(Button)
    mBtnConfirm: Button = null;

    private mCallBack = null;
    private mNdVideo = null;
    private mType: UnlockType = null;
    OnBtnClose() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        this.node.active = false;
    }

    InitPop(type: UnlockType, needVaule: number, text: string, callBack: () => void) {
        this.mCallBack = callBack;
        this.mLbContent.string = text;
        this.mType = type;
        this.UnlockingDetermination(type, needVaule);
    }

    protected onLoad(): void {
        this.mNdVideo = this.mBtnConfirm.node.getChildByName("label").getChildByName("video");
    }

    UnlockingDetermination(type, value) {
        this.mBtnConfirm.interactable = false;
        this.mNdVideo.active = false;
        switch (type) {
            case UnlockType.monsterKills:
                this.mNdspend.active = false
                if (GameData.playerData.KillMonsters >= value) {
                    this.mBtnConfirm.interactable = true;
                }
                break;
            case UnlockType.watchingAdvertisements:
                this.mNdspend.active = false;
                this.mNdVideo.active = true;
                this.mBtnConfirm.interactable = true;
                break;
            case UnlockType.spendingGold:
                this.mNdspend.active = true;
                let nd = this.mNdspend.getChildByName("btn_icon_gold");
                this.mNdspend.getChildByName("free").active = false;
                nd.children[0].getComponent(Label).string = value;
                nd.active = true;
                if (GameData.playerData.Gold >= value) {
                    this.mBtnConfirm.interactable = true;
                }
                break;
            case UnlockType.loginFollowingDay:
                this.mNdspend.active = false
                break;
            case UnlockType.upgradeLevel:
                this.mNdspend.active = false
                break;
        }
        this.mBtnConfirm.node.getComponent(Sprite).grayscale = !this.mBtnConfirm.interactable
    }

    OnBtnConfirm() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (this.mType == UnlockType.watchingAdvertisements) {
            AppControll.Platform.ShowRewardVideoAd(this, (bool: boolean, str: string) => {
                if (!bool) {
                    PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                        node.getComponent(SystemMessage).InitContent(str);
                    })
                    return;
                }
                AppControll.Platform.SendBuryPoint(100012);
                if (this.mCallBack) this.mCallBack();
                if (this.node) this.node.active = false;
            })
        } else {
            if (this.mCallBack) this.mCallBack();
            this.node.active = false;
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
